package com.syc.test;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * @author hhj
 * @description
 * @date 2021-11-11 10:11
 * @menu
 */
public class TryUtil {


    public static <T, U, R> R getValue(T obj, U param, BiFunction<T, U, R> function, R defaultValue) {
        try {
            return function.apply(obj, param);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static <T, U, R> R getValue(T obj, U param, BiFunction<T, U, R> function) {
        try {
            return function.apply(obj, param);
        } catch (Exception e) {
            return null;
        }
    }

    public static <T, R> R getValue(T obj, Function<T, R> function) {
        try {
            return function.apply(obj);
        } catch (Exception e) {
            return null;
        }
    }

    public static <T, R> R getValue(T obj, Function<T, R> function, R defaultValue) {
        try {
            return function.apply(obj);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static boolean equals(Object o1, Object o2) {
        try {
            return o1.equals(o2);
        } catch (NullPointerException e) {
            return false;
        }
    }


}
